package com.edev.support.dao.impl;

import com.edev.support.dao.BasicDao;
import com.edev.support.entity.Entity;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * The decorator for BasicDao, that it's subclass can extend all kinds of functions for BasicDao.
 * @author fangang
 * @see com.edev.support.cache.CacheEntityDao
 * @see com.edev.support.ddd.Repository
 * @see com.edev.support.subclass.SubClassPlusDao
 */
/**
 * BasicDao装饰器基类（抽象类）
 * <p>
 * 核心作用：
 * 1. 为BasicDao提供装饰器模式的实现基础
 * 2. 通过继承实现各种功能扩展（如缓存、子类处理等）
 * 3. 可以通过各种子类的拼装，实现更多更复杂的功能
 * 4. 保持与原始DAO接口的兼容性
 * <p>
 * 设计模式：
 * - 采用装饰器模式（Decorator Pattern）
 * - 所有操作默认委托给被装饰的BasicDao实例
 * <p>
 * 典型子类实现：
 * - CacheEntityDao：添加缓存层
 * - Repository：实现DDD仓库的管理模式
 * - SubClassPlusDao：处理有子类继承关系的持久化操作
 * <p>
 * 使用示例：
 * <pre>
 * // 创建基础DAO
 * BasicDao basicDao = new JdbcTemplateDao();
 * // 添加缓存装饰
 * BasicDao cachedDao = new CacheEntityDao(basicDao);
 * // 添加子类处理装饰
 * BasicDao finalDao = new SubClassPlusDao(cachedDao);
 * </pre>
 * @author fangang
 * @see com.edev.support.cache.CacheEntityDao
 * @see com.edev.support.ddd.Repository
 * @see com.edev.support.subclass.SubClassPlusDao
 */
public abstract class DecoratorDao implements BasicDao {
	private BasicDao dao;
	protected DecoratorDao() {}
	protected DecoratorDao(BasicDao dao) {
		this.dao = dao;
	}

	/**
	 * 获取被装饰的原始DAO实例
	 * @return 被包装的BasicDao实例
	 */
	public BasicDao getDao() {
		return dao;
	}

	@Override
	public <E extends Entity<S>, S extends Serializable> S insert(E entity) {
		return dao.insert(entity);
	}

	@Override
	public <E extends Entity<S>, S extends Serializable> void update(E entity) {
		dao.update(entity);
	}

	@Override
	public <E extends Entity<S>, S extends Serializable> S insertOrUpdate(E entity) {
		return dao.insertOrUpdate(entity);
	}

	@Override
	public <E extends Entity<S>, S extends Serializable> void delete(E entity) {
		dao.delete(entity);
	}

	@Override
	public <E extends Entity<S>, S extends Serializable, C extends Collection<E>> void insertOrUpdateForList(C list) {
		dao.insertOrUpdateForList(list);
	}

	@Override
	public <E extends Entity<S>, S extends Serializable, C extends Collection<E>> void deleteForList(C list) {
		dao.deleteForList(list);
	}

	@Override
	public <E extends Entity<S>, S extends Serializable> void delete(S id, Class<E> clazz) {
		dao.delete(id, clazz);
	}

	@Override
	public <E extends Entity<S>, S extends Serializable> E load(S id, Class<E> clazz) {
		return dao.load(id, clazz);
	}

	@Override
	public <E extends Entity<S>, S extends Serializable> void deleteForList(Collection<S> ids, Class<E> clazz) {
		dao.deleteForList(ids, clazz);
	}

	@Override
	public <E extends Entity<S>, S extends Serializable> Collection<E> loadForList(Collection<S> ids, Class<E> clazz) {
		return dao.loadForList(ids, clazz);
	}

	@Override
	public <E extends Entity<S>, S extends Serializable> Collection<E> loadAll(E template) {
		return dao.loadAll(template);
	}

	@Override
	public <E extends Entity<S>, S extends Serializable> Collection<E> loadAll(List<Map<Object, Object>> colMap, Class<E> clazz) {
		return dao.loadAll(colMap, clazz);
	}
}